import csv
import os, sys
import random

class SystemState:
    Initialized = "Initialized"
    InCall = "InCall"
    Multisite = "Multisite"

class CallStatus:
    Connected = "Connected"
    Connecting = "Connecting"
    OnHold = "OnHold"
    Ringing = "Ringing"

class PresentationMode:
    Off = "Off"
    Sending = "Sending"
    Receiving = "Receiving"

class QueryXpath:
    PhonebookContactName = "/Command/PhonebookContactAddResult/Name"
    PhonebookSearchContact = "/Command/PhonebookSearchResult/ResultSet/Contact"
    DialResultCallId = "/Command/DialResult/CallId"
    DialResultConferencedId	= "/Command/DialResult/ConferenceId"
    # SearchResult
    SearchResultContact = "/SearchResult/PhoneBookEntries/Contact"

class QueryHistory:
    All = "History"
    CallLogs = "CallLogs/Call"
    DisconnectCause = "CallLogs/Call/DisconnectCause"
    DisconnectCauseValue = "CallLogs/Call/DisconnectCauseValue"
    MissedCallValue = "CallLogs/Missed/NewCounter"
    HistoryCallBackNumber = "CallLogs/Call/CallbackNumber"

class QueryConfiguration:
    All = "Configuration"
    SipUri = "SIP/Profile/URI"
    ProvisioningUrl	= "Provisioning/ExternalManager/Address"
    VideoNumber = "Provisioning/LoginName"
    FullProvisioning = "Provisioning"
    SystemName = "SystemUnit/Name"
    PhonebookURL = "Phonebook/Server/URL"
    
class QueryStatus:
    All = "Status"
    CallInfo = "Call"
    RemoteNumber = "Call/RemoteNumber"
    PlaceOnHold = "Call/PlacedOnHold"
    CallStatus = "Call/Status"
    TransmitCallRate = "Call/TransmitCallRate"
    ReceiveCallRate = "Call/ReceiveCallRate"
    CallEncryptionType = "Call/Encryption/Type"
    VideoResolutionX = "MediaChannels/Call/IncomingVideoChannel/Video/ResolutionX"
    VideoResolutionY = "MediaChannels/Call/IncomingVideoChannel/Video/ResolutionY"
    PresentationMode = "Conference/Presentation/Mode"
    IncomingVideoPackets = "Diagnostics/Call/Channels/IncomingVideoChannel/Netstat/Packets"
    Version = "SystemUnit/Software/Version"
    SystemState = "SystemUnit/State/System"
    ProductId = "SystemUnit/ProductId"
    
    
class XmlCommand:
    AddFavorite = "<Command><Phonebook><Contact><Add><Name>%s</Name><Number>%s</Number></Add></Contact></Phonebook></Command>"
    DeleteFavorite = "<Command><Phonebook><Contact><Delete><ContactId>%s</ContactId></Delete></Contact></Phonebook></Command>"
    Search = "<Command><PhoneBook><Search></Search></PhoneBook></Command>"
    MakeCall = "<Command><Dial Command=\"True\"><Number>%s</Number><Protocol>sip</Protocol></Dial></Command>"
    AcceptCall = "<Command><Call><Accept Command=\"True\"><CallId>%s</CallId></Accept></Call></Command>"
    AcceptAll = "<Command><Call><Accept Command=\"True\"></Accept></Call></Command>"
    RejectCall = "<Command><Call><Reject Command=\"True\"><CallId>%s</CallId></Reject></Call></Command>"
    HangUpCall = "<Command><Call><DisconnectAll Command=\"True\"></DisconnectAll></Call></Command>"
    HoldCall = "<Command><Call><Hold Command=\"True\"><CallId>%s</CallId></Hold></Call></Command>"
    ResumeCall = "<Command><Call><Resume Command=\"True\"><CallId>%s</CallId></Resume></Call></Command>"
    ClearCallLog = "<Command><CallLog><Clear Command=\"True\"></Clear></CallLog></Command>"
    StartPresentation = "<Command><Presentation><Start command=\"True\"></Start></Presentation></Command>"
    StopPresentation = "<Command><Presentation><Stop command=\"True\"></Stop></Presentation></Command>"

class XmlConfiguration:
    AutoAnswerMode = "<Configuration><Conference><AutoAnswer><Mode>%s</Mode></AutoAnswer></Conference></Configuration>"
    AutoAnswerMute = "<Configuration><Conference><AutoAnswer><Mute>%s</Mute></AutoAnswer></Conference></Configuration>"
    InternalSpeakerMode	= "<Configuration><Audio><InternalSpeaker><Mode>%s</Mode></InternalSpeaker></Audio></Configuration>"
    EncryptionMode = "<Configuration><Conference><Encryption><Mode>%s</Mode></Encryption></Conference></Configuration>"
    ProvisioningMode = "<Configuration><Provisioning><Mode>CallWay</Mode></Provisioning></Configuration>"
    ProvisioningUser = "<Configuration><Provisioning><LoginName>%s</LoginName></Provisioning></Configuration>"
    ProvisioningPassword = "<Configuration><Provisioning><Password>%s</Password></Provisioning></Configuration>"
    ProvisioningMethod = "<Configuration><Provisioning><HttpMethod>%s</HttpMethod></Provisioning></Configuration>"
    ProvisioningAddress = "<Configuration><Provisioning><ExternalManager><Address>%s</Address></ExternalManager></Provisioning></Configuration>"
    ProvisioningProtocol = "<Configuration><Provisioning><ExternalManager><Protocol>%s</Protocol></ExternalManager></Provisioning></Configuration>" 
    ProvisioningPath = "<Configuration><Provisioning><ExternalManager><Path>%s</Path></ExternalManager></Provisioning></Configuration>"
    ProvisioningDomain = "<Configuration><Provisioning><ExternalManager><Domain>%s</Domain></ExternalManager></Provisioning></Configuration>"

class EndPoint:

    def __init__(self):
        #init value
        self.uri = ""
        self.Platform = ""
        self.IP = ""
        self.Active = ""
        self.Provisioning = ""
        self.Username = ""
        self.Password = ""
        self.Version = ""
    
    def get_caller(self, excel_file, name):        
        #read source file
        rs = csv.reader(open(os.path.realpath(excel_file), "rb"))
        
        #check and get device
        for row in rs:            
            #print 'row = ' + row[0]
            if row[0] == name and row[2] == "1":
                #print row    
                self.uri = ""                
                self.Platform = row[0]
                self.IP = row[1]
                self.Active = row[2]
                self.Provisioning = row[3]
                self.Username = row[4]
                self.Password = row[5]
                self.Version = row[6]
        return self
        
    def get_callee(self, excel_file, *name):        
        data = []
        #read source file
        rs = csv.reader(open(os.path.realpath(excel_file), "rb"))
        for row in rs:
            if row[0] not in name and row[2] == "1":
                data.append(row)
                #print row
        if data:
            callee = random.sample(data, 1)
            #return value
            self.uri = ""
            self.Platform = callee[0][0]
            self.IP = callee[0][1]
            self.Active = callee[0][2]
            self.Provisioning = callee[0][3]
            self.Username = callee[0][4]
            self.Password = callee[0][5]
            self.Version = callee[0][6]
        return self

        
Caller = EndPoint()
Caller1 = EndPoint()
Callee1 = EndPoint()
Callee2 = EndPoint()
Callee3 = EndPoint()
Callee4 = EndPoint()
Callee5 = EndPoint()
Callee6 = EndPoint()
